home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / c2man-2.0pl33.lha / c2man-2.0 / strappend.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-14  |  1.3 KB  |  65 lines

  1. /* $Id: strappend.c,v 2.0.1.2 1994/01/07 07:05:39 greyham Exp $
  2.  */
  3. #include "c2man.h"
  4. #include "strappend.h"
  5.  
  6. #ifdef I_STDARG
  7. #include <stdarg.h>
  8. #endif
  9. #ifdef I_VARARGS
  10. #include <varargs.h>
  11. #endif
  12.  
  13. extern void outmem();
  14.  
  15. /*
  16.  * append a list of strings to another, storing them in a malloc'ed region.
  17.  * The first string may be NULL, in which case the rest are simply concatenated.
  18.  */
  19. #ifdef I_STDARG
  20. char *strappend(char *first, ...)
  21. #else
  22. char *strappend(va_alist)
  23.     va_dcl
  24. #endif
  25. {
  26.     size_t totallen;
  27.     va_list argp;
  28.     char *s, *retstring;
  29. #ifndef I_STDARG
  30.     char *first;
  31. #endif    
  32.     /* add up the total length */
  33. #ifdef I_STDARG
  34.     va_start(argp,first);
  35. #else
  36.     va_start(argp);
  37.     first = va_arg(argp, char *);
  38. #endif
  39.     totallen = first ? strlen(first) : 0;
  40.     while ((s = va_arg(argp,char *)) != NULL)
  41.     totallen += strlen(s);
  42.     va_end(argp);
  43.     
  44.     /* malloc the memory */
  45.     totallen++;    /* add space for the nul terminator */
  46.     if ((retstring = first ? realloc(first,totallen) : malloc(totallen)) == 0)
  47.     outmem();
  48.  
  49.     if (first == NULL)    *retstring = '\0';
  50.  
  51. #ifdef I_STDARG
  52.     va_start(argp,first);
  53. #else
  54.     va_start(argp);
  55.     first = va_arg(argp, char *);    /* skip the first arg */
  56. #endif
  57.  
  58.     while ((s = va_arg(argp,char *)) != NULL)
  59.     strcat(retstring,s);
  60.  
  61.     va_end(argp);
  62.  
  63.     return retstring;
  64. }
  65.